package Runde2;

import java.util.*;

public class UE extends LVA {
	
	private HashSet<Person> tutors;
	private ArrayList<Group> groups;
	
	/**
	 * Creates new UE.
	 * 
	 * @param lvaNr Unique course number.
	 * @param title Title of the course.
	 * @param earliestReg Earliest date to register.  
	 * @param latestReg Latest date to register.
	 * @param deReg Latest date to deregister.
	 * @param maxMembers Maximum number of members (unlimited = Integer.MAX_VALUE). 
	 * @throws InvalidLVAException Numerous error cases (end of registration precedes its beginning, ...)
	 * 
	 */
	public UE(int lvaNr, String title, Person owner,
			GregorianCalendar earliestReg, GregorianCalendar latestReg,
			GregorianCalendar deReg, int maxMembers, ArrayList<Integer> predecessors, int[] requirements)
			throws InvalidLVAException {
		super(lvaNr, title, owner, earliestReg, latestReg, deReg, maxMembers, predecessors,
				requirements);
		
		tutors = new HashSet<Person>();
		groups = new ArrayList<Group>();
	}	
	
	/**
	 * Adds a new group to the course.
	 * 
	 * @param pw The owner's password.
	 * @param tutor The supervisor of this group.
	 * @throws InsufficientPermissionsException Thrown when the password is wrong.
	 */
	public int addGroup(String pw, Person tutor, int maxParticipants) throws InsufficientPermissionsException {
		if(!tutors.contains(tutor)) {	
			throw new InsufficientPermissionsException("Person is not a tutor.");
		}
		
		try {
			Group newGroup = new Group(tutor, getNr(), maxParticipants, pw);
			groups.add(newGroup);
			return groups.indexOf(newGroup);
		} catch (InsufficientPermissionsException e) {
			throw new InsufficientPermissionsException(e.getMessage());
		}
	}
	
	/**
	 * Adds a tutor to the course.
	 * 
	 * @param pw The owner's password.
	 * @param tutor The person to add.
	 * @throws InsufficientPermissionsException Thrown when the password is wrong.
	 */
	public void addTutor(String pw, Person tutor) throws InsufficientPermissionsException {
		if(tutor == null) {
			throw new InsufficientPermissionsException("Can't add tutor: unkown person.");
		}
		
		if(getOwner().validate(pw)) {
			tutors.add(tutor);
		} else {
			throw new InsufficientPermissionsException("Wrong password. (lva-owner)");
		}
	}
	
	/**
	 * Getter-function for the list of groups.
	 * 
	 * @param index The group's number.
	 * @return The requested group.
	 */
	public Group getGroup(int index) {
		return groups.get(index);
	}
	
	/**
	 * Removes a group from the course.
	 * 
	 * @param pw The owner's password.
	 * @param group The group to remove.
	 * @throws InsufficientPermissionsException Thrown when the password is wrong.
	 */
	public void removeGroup(String pw, Group group) throws InsufficientPermissionsException {
		if(getOwner().validate(pw)) {
			groups.remove(group);
		} else {
			throw new InsufficientPermissionsException("Wrong password. (lva-owner)");
		}
	}
	
	/**
	 * Removes a tutor from the course.
	 * 
	 * @param pw The owner's password.
	 * @param tutor The person to remove.
	 * @throws InsufficientPermissionsException Thrown when the password is wrong.
	 */
	public void removeTutor(String pw, Person tutor) throws InsufficientPermissionsException {
		if(getOwner().validate(pw)) {
			tutors.remove(tutor);
		} else {
			throw new InsufficientPermissionsException("Wrong password. (lva-owner)");
		}
	}
}